package com.bw.lib_fan

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.bottomnavigation.BottomNavigationView

@Route(path = "/fan/LinkageActivity")
class LinkageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_linkage)
        // 获取当前 Activity 的完整类名并打印日志
        val currentActivityPath = javaClass.name
        Log.d("CurrentActivity", currentActivityPath)

        val viewPager: ViewPager2 = findViewById(R.id.viewPager)
        val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottom_navigation)
        // 设置 ViewPager2 的适配器
        val adapter = ViewPagerAdapter(this)
        viewPager.adapter = adapter
        // 监听 ViewPager2 的页面变化事件，并更新 BottomNavigationView 的选中项
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                // 根据当前页面位置设置 BottomNavigationView 的选中项
                bottomNavigationView.menu.getItem(position).isChecked = true
            }
        })

        // 监听 BottomNavigationView 的点击事件，并更新 ViewPager2 的当前页面
        bottomNavigationView.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_first -> {
                    viewPager.currentItem = 0
                    true
                }
                R.id.navigation_second -> {
                    viewPager.currentItem = 1
                    true
                }
                R.id.navigation_third -> {
                    viewPager.currentItem = 2
                    true
                }
                else -> false
            }
        }
    }
}